//
//  EditImageViewController.m
//  EditImage
//
//  Created by Ngoc on 22/12/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "EditImageViewController.h"
#import "BackgroundToolbar.h"
#import "SHK.h"
#import "SHKFacebook.h"

@interface EditImageViewController( Private)

-(void) undoHandler:(id) sender;
-(void) redoHandler:(id) sender;
-(void) doneHandler:(id) sender;
-(void) showSaveMenu:(id) sender;
-(void) cancelHandler:(id) sender;
@end

@implementation EditImageViewController
@synthesize popoverController =_popover;
@synthesize delegate = _delegate;
@synthesize currentImage;
-(id) initWithImage:(UIImage *)image delegate:(id)delegate
{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _originalImage = image;
    }
    return self;
}
-(void) viewDidLoad
{
    _rollMenu = [[DnRollMenu alloc]initWithFrame:CGRectMake(8, 460-100, 320, 200)];
    _rollMenu.dataSource = self;
    [self.view addSubview:_rollMenu];
    
    //UIImage *testimage = [UIImage imageNamed:@"103.jpg"];
    _imageView = [[ DnSuperImageView alloc] initWithFrame:CGRectMake(9, 57, 298, 295) image:_originalImage];
    [self.view addSubview:_imageView]; 
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg.png"]];
    
    UIBarButtonItem *bbtnSpace1, *bbtnSpace2;
    _toolbar = [[BackgroundToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,44)];
    _bbtnRedo = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRedo target:self action:@selector(redoHandler:)];
    _bbtnUndo = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoHandler:)];
    _bbtnSave = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(showSaveMenu:)];
    _bbtnRedo.enabled = NO;
    _bbtnUndo.enabled = NO;
    _bbtnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneHandler:)];
    _bbtnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelHandler:)];
    bbtnSpace1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    bbtnSpace2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    _redoUndoToolbarItems = [[NSArray arrayWithObjects:_bbtnUndo,bbtnSpace1,_bbtnSave,bbtnSpace2,_bbtnRedo, nil]retain];
    _doneCancelToolbarItems  = [[NSArray arrayWithObjects:_bbtnCancel,bbtnSpace2, _bbtnDone, nil]retain];
    [_toolbar setItems:_redoUndoToolbarItems];
    [self.view addSubview:_toolbar];
}
-(UIImage*) currentImage
{
    return _imageView.image;
}
-(void) hideSaveMenu
{
    if (self.popoverController) {
		[self.popoverController dismissPopoverAnimated:YES];
		self.popoverController = nil;
    }
}
-(void) showSaveMenu:(id) sender
{
    int itemWidth = 22;
    if (self.popoverController) {
		//if (!self.popoverController.popoverVisible) {
            [self.popoverController presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2-itemWidth,0,50, 40) 
                                                    inView:self.view 
                                  permittedArrowDirections:UIPopoverArrowDirectionUp
                                                  animated:YES];
        //}
        
	} else {
		UIViewController *contentViewController = [[WEPopoverContentViewController alloc] initWithStyle:UITableViewStylePlain];
		((WEPopoverContentViewController*)contentViewController).delegate = self;
		self.popoverController = [[[WEPopoverController alloc] initWithContentViewController:contentViewController] autorelease];
        
		[self.popoverController presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2-itemWidth,0,50, 40) 
												inView:self.view 
							  permittedArrowDirections:UIPopoverArrowDirectionUp
											  animated:YES];
		[contentViewController release];
	}
}

//============================================================================================
//======ActionMenuDelegate=======
-(NSInteger) numberOfMenuRow
{
    return 5;
}
-(UITableViewCell*) tableMenu:(UITableView*) table itemAtIndex:(NSInteger) itemIndex
{
   return  [self.delegate actionMenuAtIndex:itemIndex];
}
-(void) tableMenu:(UITableView *)table menuClickAtIndex:(NSInteger)itemIndex
{
    [self hideSaveMenu];
    [self.delegate controller:self clickAtIndex:itemIndex];
}
//============================================================================================

-(void) changeBarButtonUndoRedoState
{
    if (_imageView) {
        if ([_imageView isCanUndo]) {
            _bbtnUndo.enabled = YES;
        }
        else _bbtnUndo.enabled = NO;
        
        if ([_imageView isCanRedo]) {
            _bbtnRedo.enabled = YES;
        }
        else _bbtnRedo.enabled = NO;
    }
}

-(void) changeToolbarToActionState:(BOOL) isEnable
{
    if (isEnable) {
        
        [_toolbar setItems:_doneCancelToolbarItems animated:YES];
    }
    else 
    {
        [self changeBarButtonUndoRedoState];
     [_toolbar setItems:_redoUndoToolbarItems animated:YES];   
    }
}
-(void) showMessage:(id) sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:@"abc" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
-(void) undoHandler:(id) sender
{
    
    if (_imageView) {
        [_imageView undo];
    }
    [self changeBarButtonUndoRedoState];
}
-(void) redoHandler:(id) sender
{
    
    if (_imageView)
    {
        [_imageView redo];
    }
    [self changeBarButtonUndoRedoState];
}
-(void) doneHandler:(id) sender
{
    if (_imageView) {
        [_imageView finishCurrentAction];
    }
    [self changeToolbarToActionState:NO];
}
-(void) cancelHandler:(id) sender
{
    if(_imageView) 
    {
        [_imageView cancelCurrentAction];
    }
    [self changeToolbarToActionState:NO];
    
}
-(void) prepareForNewAction
{
    [_imageView cancelCurrentAction];
    [self changeToolbarToActionState:YES];
}
//============================================================================================
//====================Crop====================
-(void) cropMenuItemHandler:(id) sender
{
    if (!_imageView) {
        return;
    }
    ResizeMethod resizeMethod = FreeDom;
   // [_imageView cancelCurrentAction];
    [self prepareForNewAction];
    
    BOOL isApply = YES;
    switch ([(UIView*)sender tag]) {
        case 0:            
        {
            //title =  @"Straighten";
            resizeMethod = Straighten;
            break;
        }
        case 1:            
        {
            //title =  @"Freedom";
            resizeMethod = FreeDom;
            break;
        } 
        case 2:            
        {
            //title =  @"Square";
            resizeMethod = S1X1;
            break;
        }
        case 3:            
        {
            //title =  @"3x2";
            resizeMethod = S3X2;
            break;
        } 
        case 4:            
        {
            //title =  @"16x9";
            resizeMethod = S16X9;
            break;
        } 
        case 5:            
        {
            
            //title =  @"4x6";
            resizeMethod = S4X6;
            break;
        } 
        case 6:            
        {
            //title =  @"3x4";
            resizeMethod = S3X4;
            break;
        } 
        case 7:            
        {
            //title =  @"4x3";
            resizeMethod = S4X3;
            break;
        } 
        case 8:            
        {
            //title =  @"3x5";
            resizeMethod = S3X5;
            break;
        } 
        case 9:            
        {
            //title =  @"5x3";
            resizeMethod = S5X3;
            break;
        } 
        case 10:            
        {
            //title =  @"5x7";
            resizeMethod = S5X7;
            break;
        } 
        case 11:            
        {
            //title =  @"7x5";
            resizeMethod = S7X5;
            break;
        } 
        case 12:            
        {
            //title =  @"4x5";
            resizeMethod = S4X5;
            break;
        } 
        case 13:            
        {
            //title =  @"5x4";
            resizeMethod = S5X4;
            break;
        } 

        default:
            break;
    }
    if (isApply) {
        _imageView.resizeStrategy = resizeMethod;
        [_imageView startAction:ActionCrop];
    }
}
-(UIView*) getCropMenuItem:(NSInteger) itemIndex
{
    UIView *returnView=nil;
    UIImage *normalItemImg;
    NSString *title;
    DnButton *button;
    SEL selectedHandler = @selector(cropMenuItemHandler:);
    title =  @"";
    normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
    NSInteger xCor=0, yCor=0, width = kMenuItemWidth, height = kMenuItemHeight;
    switch (itemIndex) {
        case 0:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Straighten";
            break;
        }
        case 1:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Freedom";
            break;
        } 
        case 2:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Square";
            break;
        }
        case 3:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"3x2";
            break;
        } 
        case 4:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"16x9";
            break;
        }
        case 5:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"4x6";
            break;
        } 
        case 6:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"3x4";
            break;
        } 
        case 7:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"4x3";
            break;
        } 
        case 8:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"3x5";
            break;
        } 
        case 9:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"5x3";
            break;
        } 
        case 10:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"5x7";
            break;
        } 
        case 11:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"7x5";
            break;
        } 
        case 12:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"4x5";
            break;
        } 
        case 13:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"5x4";
            break;
        } 
 
    }
    button = [[DnButton alloc] initWithFrame:CGRectMake(xCor, yCor, width , height) title:title backgroundImage:normalItemImg autoResize:YES];
    button.tag = itemIndex;
    [button setTitleColor:[UIColor colorWithRed:67.0f/255.0f green:70.0f/255.0f blue:59.0f/255.0f alpha:1.0] forState:UIControlStateNormal ] ;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [button.titleLabel setFont:[UIFont fontWithName:@"Papyrus-Bold" size:16.0]];
    [button addTarget:self action:selectedHandler forControlEvents:UIControlEventTouchUpInside];
    button.tag = itemIndex;
    returnView = [button retain];
    [button release];
    return returnView;
}
//====================Ajust====================
-(void) adjustMenuItemHandler:(id) sender
{
    if (!_imageView) {
        return;
    }
    FlipDirection direction;
    //[_imageView cancelCurrentAction];
    [self prepareForNewAction];
    switch ([(UIView*)sender tag]) {
        case 0:            
        {
            //title =  @"Rotate Left";
            direction = FlipLeft;
            break;
        }
        case 1:            
        {
            //title =  @"Rotate Right";
            direction = FlipRight;
            break;
        } 
        case 2:            
        {
            //title =  @"Flip V";
            direction = FlipVertical;
            break;
        }
        case 3:            
        {
            //title =  @"Flip H";
            direction = FlipHorizontal;
            break;
        } 
    }
    [_imageView flipImage:direction];
}
-(UIView*) getAdjustMenuItem:(NSInteger) itemIndex
{
    UIView *returnView=nil;
    UIImage *normalItemImg;
    NSString *title;
    DnButton *button;
    SEL selectedHandler=@selector(adjustMenuItemHandler:);
    title =  @"";
    normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
    NSInteger xCor=0, yCor=0, width = kMenuItemWidth, height = kMenuItemHeight;
    switch (itemIndex) {
        case 0:            
        {
            xCor = 6;
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Rotate Left";
            break;
        }
        case 1:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Rotate Right";
            break;
        } 
        case 2:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Flip V";
            break;
        }
        case 3:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Flip H";
            break;
        } 
    }
    button = [[DnButton alloc] initWithFrame:CGRectMake(xCor, yCor, width , height) title:title backgroundImage:normalItemImg autoResize:YES];
    button.tag = itemIndex;
    [button setTitleColor:[UIColor colorWithRed:67.0f/255.0f green:70.0f/255.0f blue:59.0f/255.0f alpha:1.0] forState:UIControlStateNormal ] ;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [button.titleLabel setFont:[UIFont fontWithName:@"Papyrus-Bold" size:16.0]];
    [button addTarget:self action:selectedHandler forControlEvents:UIControlEventTouchUpInside];
    button.tag = itemIndex;
    returnView = [button retain];
    [button release];
    return returnView;
    
}
//====================Effects====================
-(void) effectMenuItemHandler:(id) sender
{
    if (!_imageView) {
        return;
    }
    FilterAlgorithm fliter;
    //[_imageView cancelCurrentAction];
    [self prepareForNewAction];
    switch ([(UIView*) sender tag]) {  
        case 0:
        {
//            title =  @"TintRed";
            fliter = TintRed;
            break;
        }
        case 1:
        {
//            title =  @"Greyscale";
            fliter = Greyscale;
            break;
        }
        case 2 :
        {
//            title =  @"Pixelate";
            fliter = Pixelate;
            break;
        }
        case 3:
        {
//            title =  @"Brightness";
            fliter = Brightness;
            break;
        }
        case 4:
        {
//            title =  @"Saturation";
            fliter = Saturation;
            break;
        }
        case 5 :
        {
//            title =  @"Hue";
            fliter = Hue;
            break;
        }
        case 6:
        {
//            title =  @"Thermal";
            fliter = Thermal;
            break;
        }
        case 7:
        {
//            title =  @"SnowFuzz";
            fliter = SnowFuzz;
            break;
        }
        case 8:
        {
//            title =  @"Blur";
            fliter = Blur;
            break;
        }
        case 9:
        {
//            title =  @"FindEdges";
            fliter = FindEdges;
            break;
        }
        case 10:
        {
//            title =  @"Emboss";
            fliter = Emboss;
            break;
        }
        case 11:
        {
//            title =  @"Sharpen";
            fliter = Sharpen;
            break;
        }
        case 12 :
        {
//            title =  @"TiltShift";
            fliter = TiltShift;
            break;
        }   
        default:
            break;
    }
    _imageView.fliter = fliter;
    [_imageView startAction:ActionFilter];
}
-(UIView*) getEffectMenuItem:(NSInteger) itemIndex
{
    UIView *returnView=nil;
    UIImage *normalItemImg;
    NSString *title;
    DnButton *button;
    SEL selectedHandler = @selector(effectMenuItemHandler:);
    title =  @"";
    normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
    NSInteger xCor=0, yCor=0, width = kMenuItemWidth, height = kMenuItemHeight;
    switch (itemIndex) {  
        case 0:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"TintRed";
            //filter = TintRed;
            break;
        }
        case 1:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Greyscale";
            //filter = Greyscale;
            break;
        }
        case 2 :
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Pixelate";
            //filter = Pixelate;
            break;
        }
        case 3:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Brightness";
            //filter = Brightness;
            break;
        }
        case 4:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Saturation";
            //filter = Saturation;
            break;
        }
        case 5 :
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Hue";
            //filter = Hue;
            break;
        }
        case 6:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Thermal";
           // filter = Thermal;
            break;
        }
        case 7:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"SnowFuzz";
            //filter = SnowFuzz;
            break;
        }
        case 8:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Blur";
            //filter = Blur;
            break;
        }
        case 9:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"FindEdges";
            //filter = FindEdges;
            break;
        }
        case 10:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Emboss";
            //filter = Emboss;
            break;
        }
        case 11:
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Sharpen";
            //filter = Sharpen;
            break;
        }
        case 12 :
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"TiltShift";
            //filter = TiltShift;
            break;
        }   
        default:
            break;
    }
    button = [[DnButton alloc] initWithFrame:CGRectMake(xCor, yCor, width , height) title:title backgroundImage:normalItemImg autoResize:YES];
    button.tag = itemIndex;
    [button setTitleColor:[UIColor colorWithRed:67.0f/255.0f green:70.0f/255.0f blue:59.0f/255.0f alpha:1.0] forState:UIControlStateNormal ] ;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [button.titleLabel setFont:[UIFont fontWithName:@"Papyrus-Bold" size:16.0]];
    [button addTarget:self action:selectedHandler forControlEvents:UIControlEventTouchUpInside];
    button.tag = itemIndex;
    returnView = [button retain];
    [button release];
    return returnView;
    
}

//====================Borders====================
-(void) borderMenuItemHandler:(id) sender
{
    [self prepareForNewAction];
    switch ([(UIView*)sender tag] ) {
        case 0:            
        {
            [_imageView borderWithStyle:DnBorderStyleRoundCorner];
            break;
        }
        case 1:            
        {
            [_imageView borderWithStyle:DnBorderStyleBlackRectBound];
            break;
        }
        case 2:            
        {
            [_imageView borderWithStyle:DnBorderStyleWhiteRectBound];
            break;
        }
        case 3:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle1];
            break;
        }
        case 4:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle2];
            break;
        }
        case 5:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle3];
            break;
        }
        case 6:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle4];
            break;
        }
        case 7:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle5];
            break;
        }
        case 8:            
        {
            [_imageView borderWithStyle:DnBorderStyleSpecialStyle6];
            break;
        }
            
    }

}
-(UIView*) getBorderMenuItem:(NSInteger) itemIndex
{
    UIView *returnView=nil;
    UIImage *normalItemImg;
    NSString *title;
    DnButton *button;
    SEL selectedHandler=@selector(borderMenuItemHandler:);
    title =  @"";
    normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
    NSInteger xCor=0, yCor=0, width = kMenuItemWidth, height = kMenuItemHeight;
    switch (itemIndex) {
        case 0:            
        {
            xCor = 6;
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Round Corner";
            //selectedHandler = @selector(showMessage:);
            break;
        }
        case 1:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Black Bound";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
        case 2:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"White Bound";
            //selectedHandler = @selector(showMessage:);
            break;
        }
        case 3:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 1";
            //selectedHandler = @selector(showMessage:);
            break;
        }
        case 4:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 2";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
        case 5:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 3";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
        case 6:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 4";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
        case 7:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 5";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
        case 8:            
        {
            normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
            title =  @"Special 6";
            //selectedHandler = @selector(showMessage:);
            break;
        } 
    }
    button = [[DnButton alloc] initWithFrame:CGRectMake(xCor, yCor, width , height) title:title backgroundImage:normalItemImg autoResize:YES];
    button.tag = itemIndex;
    [button setTitleColor:[UIColor colorWithRed:67.0f/255.0f green:70.0f/255.0f blue:59.0f/255.0f alpha:1.0] forState:UIControlStateNormal ] ;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [button.titleLabel setFont:[UIFont fontWithName:@"Papyrus-Bold" size:16.0]];
    [button addTarget:self action:selectedHandler forControlEvents:UIControlEventTouchUpInside];
    button.tag = itemIndex;
    returnView = [button retain];
    [button release];
    return returnView;
    
}
-(UIView*) getDemoItemView:(NSInteger) menuIndex itemIndex:(NSInteger) itemIndex
{
    UIImage *normalItemImg = [UIImage imageNamed: @"19-gear.png" ];
    NSString *title;
    DnButton *button;
    title =  [NSString stringWithFormat:@"%d - %d",menuIndex,itemIndex];
    button = [[DnButton alloc] initWithFrame:CGRectMake(0, 0, kMenuItemWidth , kMenuItemHeight) title:title backgroundImage:normalItemImg autoResize:YES];
    
    [button setTitleColor:[UIColor colorWithRed:67.0f/255.0f green:70.0f/255.0f blue:59.0f/255.0f alpha:1.0] forState:UIControlStateNormal ] ;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [button.titleLabel setFont:[UIFont fontWithName:@"Papyrus-Bold" size:16.0]];
    
    return button;
}
//==========================================================================================

- (UIColor *) randomColor {
    CGFloat red =  (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
    CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
    return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
// Menu Datasource
-(UIView*) menu:(NSInteger) menuIndex itemAtIndex:(NSInteger) itemIndex
{
    UIView *returnView;
    switch (menuIndex) {
        case 0:
            returnView = [self getCropMenuItem:itemIndex];
            break;
        case 1:
            returnView = [self getAdjustMenuItem:itemIndex];
            break;
        case 2:
            returnView = [self getEffectMenuItem:itemIndex];
            break;
        case 3:
            returnView = [self getBorderMenuItem:itemIndex];
            break;        
    }
    return returnView;
}

-(UIView*) navigationMenuItemAtIndex:(NSInteger)itemIndex
{
    UIView *returnView;
    UIButton *button;
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    //button = [[GradientButton alloc]initWithFrame:CGRectMake(0, 0, KMenuNavigationItemWidth , kMenuNavigationItemHeight)];
    button.frame = CGRectMake(0, 0, KMenuNavigationItemWidth , kMenuNavigationItemHeight);
    //NSString *title = [NSString stringWithFormat:@"Menu %d",itemIndex]; 
    //[button setTitleColor:[self randomColor] forState:UIControlStateNormal ] ;
    //button.backgroundColor = [UIColor clearColor];
    UIImage *normalImage, *selectedImage;
    switch (itemIndex) 
    { 
        case 0:
        {
            //title = @"Crop";
            selectedImage = [UIImage imageNamed:@"crops.png"];
            normalImage = [UIImage imageNamed:@"crops_sl.png"];
            break;
        }
        case 1:
        {
            //title = @"Adjust";
            normalImage = [UIImage imageNamed:@"adjust.png"];
            selectedImage = [UIImage imageNamed:@"adjust_sl.png"];            
            break;
        }
        case 2:
        {
            //title = @"Effects";
            normalImage = [UIImage imageNamed:@"effects.png"];
            selectedImage = [UIImage imageNamed:@"effects_sl.png"];            
            break;
        }
        case 3:
        {
            //title = @"Border";
            normalImage = [UIImage imageNamed:@"borders.png"];
            selectedImage = [UIImage imageNamed:@"borders_sl.png"];            
            break;
        }
    }
    //[button setTitle:title forState:UIControlStateNormal];
    [button setImage:normalImage forState:UIControlStateNormal];
    [button setImage:selectedImage forState:UIControlStateSelected ];
    returnView = button;
    return returnView;
    
}
-(NSInteger) numberOfMenus
{
    return 4;
}
-(NSInteger) numberItemsOfMenu:(NSInteger) menuIndex
{
    NSInteger count =0;
    switch (menuIndex) {
            // Crops
        case 0:
        {
            count = 14;
            break;
        } 
            // Adjust
        case 1:
        {
            count = 4;
            break;
        }
            //Effects
        case 2:
        {
            count = 13;
            break;
        }
            // borders
        case 3:
        {
            count = 9;
            break;
        }
    }
    return count;
}

-(BOOL) canBecomeFirstResponder
{
    return YES;
}
- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
